package com.bawan.utils;

import javax.naming.*;
import javax.sql.*;
import java.sql.Connection;

/**
 * Desc:  数据库连接池
 * Author: zhengcongchun
 * CreateTime: 2014-04-22 10:21
 * LastModifyTime: 2014-04-22 10:21
 * FinalModifier: zhengcongchun
 * Version: 1.0
 */
public class DBPoolUtil {
    Connection connection = null;
    private String source = "";

    public DBPoolUtil() throws Exception {
        source = "java:com/env/jdbc/office";
        Context ctx = new InitialContext();
        DataSource dataSource = (DataSource)ctx.lookup(source);
        connection = dataSource.getConnection();
    }

    //重载构造方法：连接其他数据源
    public DBPoolUtil(String source) throws Exception {
        this.source = source;
        Context context = new InitialContext();
        DataSource dataSource = (DataSource)context.lookup(source);
        connection = dataSource.getConnection();
    }

    private void makeConnection() throws Exception  {
        Context context = new InitialContext();
        DataSource dataSource = (DataSource)context.lookup(source);
        connection = dataSource.getConnection();
    }

    public void closeConn() throws Exception {
        if (connection != null && !connection.isClosed())
            connection.close();
    }
}
